
$(function() {	
	Tip.emp('负责人');
	
	$("input[id^='code_s']:visible").autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: 'car.php?do=Cnc.ajax_code',
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							label: item.code + " [" + item.name + "]",
							value: item.code + ":" + item.name
						}
					}));
				}
			});
		},
		
		minLength: 1
	});
});

var Cnc = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}
				
				var form = $("#form_add")[0];

				var body = { };
				
				body.emp = form.emp.value;
				body.date = form.date.value;
				
				// body.supplier = form.supplier.value;				
				// body.supplier_x = Supplier.supplier_x(form);
				
				body.code = form.code.value;
				body.name = form.name.value;
				body.content = form.content.value;
				
				body.machine1 = form.machine1.value;
				body.machine2 = form.machine2.value;

				// body.ext_x = Cnc.ext_x(form);

				$(this).dialog("close");

				$.post('car.php?do=Cnc.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'car.php?do=Cnc.edit&id=' + id + p2;
		var c2 = 'car.php?do=Cnc.view&id=' + id;
		
		var a1 = ['编辑信息', 'url', 'Cnc.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '600x400'];
		
		var c4 = 'car.php?do=Maintain.add&id=' + id;
		var c5 = 'car.php?do=Cnc.status&id=' + id;

		var a4 = ['车辆服务', 'url', 'Cnc.dialog_maintain', '600x400'];
		var a5 = ['恢复正常', 'url', 'Cnc.list_ok'];

		var s = c1 + ", " + c2;

		return this.popup_(e, [a1, a2], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var c1 = 'car.php?do=Cnc.upload&id=' + id;
		var a1 = ['上传附件', 'url', 'Common.dialog_upload', '600x420'];
		
		var c2 = 'car.php?do=Cnc.copy&id=' + id;
		var a2 = ['复制信息', 'url', 'Cnc.dialog_copy_', '600x400'];
		
		var ss = 'car.php?do=Cnc.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Cnc.dialog_ss', '400x200'];
		
		s += ", " + c1 + ", " + c2 + ", " + ss;
		
		a.push(a1);
		a.push(a2);
		
		a.push(bb);
		
		return popup(e, a, s);
	}, 

	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('car.php?do=Cnc.audit&item=88', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'car.php?do=Cnc.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	}, 

	/**
	 * 编辑
	 */
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('car.php?do=Cnc.ajax_oper', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						
						body.emp = form.emp.value;
						body.date = form.date.value;
						
						// body.supplier = form.supplier.value;				
						// body.supplier_x = Supplier.supplier_x(form);
						
						body.code = form.code.value;
						body.name = form.name.value;
						body.content = form.content.value;
						
						body.machine1 = form.machine1.value;
						body.machine2 = form.machine2.value;
						
						body.status = form.status.value;

						// body.ext_x = Cnc.ext_x(form);

						// $(this).dialog("close");
						
						$('#userfile').uploadify('upload');

						var url = 'car.php?do=Cnc.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				
				body.emp = form.emp.value;
				body.date = form.date.value;
				
				// body.supplier = form.supplier.value;				
				// body.supplier_x = Supplier.supplier_x(form);
				
				body.code = form.code.value;
				body.name = form.name.value;
				body.content = form.content.value;
				
				body.machine1 = form.machine1.value;
				body.machine2 = form.machine2.value;

				// body.ext_x = Cnc.ext_x(form);

				$(this).dialog("close");

				var url = 'car.php?do=Cnc.addPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	download: function (key) {
		if (!confirm("确认是否下载？若下载，建议先另存，再打开。")) {
			return false;
		}
		
		var url = 'car.php?do=Cnc.download&key=' + key;
		jswin(url, 'save', '800x600');
		
		return true;
	},
	
	file: function (id, key) {
		if (!confirm("确认是否删除？")) {
			return false;
		}
		
		var c6 = 'car.php?do=Cnc.upload&id=' + id;
		
		var url = 'car.php?do=Cnc.file';
		var body = { };
		body.key = key;
		
		$.post(url, body, function(data) {
			// alert(data);
			$.get(c6, function(data) {
				$('#dialog_edit').html(data);
			});
		});
		
		return true;
	},

	dialog_maintain: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('car.php?do=Cnc.audit&item=75', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"添加": function() {
						if (!confirm("确认是否添加？")) {
							return false;
						}
						
						var form = $("#form_add")[0];

						var body = { };
						body.id = form.id.value;
						body.title = form.title.value;
						body.maintain = form.maintain.value;

						body.content = form.content.value;
						body.cost = form.cost.value;
						
						$(this).dialog("close");

						$.post('car.php?do=Maintain.addPost', body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'car.php?do=Cnc.delete' + p2;		
		var c2 = 'car.php?do=Cnc.xls&xls=2' + p2;
		var c9 = 'car.php?do=Cnc.xls' + p2;
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];				
		var a2 = ['登记表', 'url', 'jswin', '1024x768'];
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c9;

		return popup(e, [a1, a9], s);
	},
	
	ext_x: function(form) {
		
		var ret = {};	
		
		ret.park = form.park.value;
		ret.cpxh = form.cpxh.value;
		ret.nsr = form.nsr.value;
		ret.fdjh = form.fdjh.value;
		ret.cjh = form.cjh.value;
		
		return $.toJSON(ret);
	}

};
